<?php
/**
 * Created by PhpStorm.
 * User: 聂尧
 * Date: 2017/7/13
 * Time: 15:05
 * funciotn: 后台主页类主要处理主页的一些业务如权限管理才
 *           菜单项
 */
namespace app\admin\controller;
use       think\Controller;
use       think\Request;
use       think\Db;

class Index extends Controller{


    //构造函数中判断用户是不是登录了 
    public function _initialize()
    {
         session_start();
         if(empty($_SESSION['username']))
         {
            $this->success('请先登录','/admin/login/index');
         }

    }
    //后台主页函数
    public function index()
    {
      //判断这个用户是什么角色
     //var_dump($_SESSION);
     $username           =$_SESSION['username'];
     $system_user_table  = config('_systemuser_');
     $system_menu        = config('_menu_');
     $system_role        = config('_role_');
     $role_id            = Db::query("select role_id from $system_user_table where user_name=:user_name",['user_name'=>$username]);
     var_dump($role_id);
     $power              = Db::table('x_system_role')->where('r_id',$role_id[0]['role_id'])->find();
     var_dump($power);
     $power              = explode(',',$power['power']);
     $power              = array_filter($power);
      //得到菜单列表数组
      $menu=array();
      for($i=0;$i<count($power);$i++)
      {

          $data=Db::query("select * from $system_menu where pid=:pid",['pid'=>$power[$i]]);
          if($data) {
                      $menu[] = $data;
          }
          //清空数组
          unset($data);
      }
      //渲染到index页面
      $this->assign('count',count($menu));
      $this->assign('list',$menu);
      return $this->fetch();
    }

    //welcome
    public function welcome()
    {
        return $this->fetch();
    }

    //管理员列表
    public  function admin_list()
    {
        $count=Db::table('x_system_user')->count();
        $lists=Db::table('x_system_user')->paginate(6);
        $this->assign('count',$count);
        $this->assign('list', $lists);
        return $this->fetch();
    }

    //添加管理员
    public function admin_add()
    {
         return $this->fetch();
    }
    public function admin_add_data()
    {
        $data  =Request::instance()->param();

        //加入添加数据时间
        $data['create_time']=time();
        unset($data['password2']);
        $data['password']=md5($data['password']);
       $offest=Db::name('x_system_user')->insert($data);
       if($offest)
       {
         return json(['code'=>1,'msg'=>'成功添加']);
       }
         return json(['code'=>0,'msg'=>'添加失败']);
    }

    //编辑页面show
    public function admin_update_show()
    {
        $id  =Request::instance()->get('id');
        $list=Db::name('x_system_user')->where('id',$id)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }

    //更新用户信息
    public function admin_update_data()
    {
        $id=Request::instance()->get('id');
        $data  =Request::instance()->param();

        //加入添加数据时间
        $data['create_time']=time();
        $data['password']=md5($data['password']);
        unset($data['password2']);
        $offest=Db::name('x_system_user')->where('id',$id)->update($data);
        if($offest)
        {
            return json(['code'=>1,'msg'=>'更新成功!']);
        }
        return json(['code'=>0,'msg'=>'更新失败~']);

    }

    //停用一个用户
    public function is_click()
    {
         $id=Request::instance()->get('id');
         $is_click=Db::query('select is_click from x_system_user where id=:id',['id'=>$id]);
         switch($is_click)
         {
             case 0:
                 $is_click=1;
                 break;
             case 1:
                 $is_click=0;
                 break;
             default:
                 break;
         }
         //更新数据库数据
        Db::name('x_system_user')->where('id',$id)->update(['is_click'=>$is_click]);
        return json(['code'=>1]);

    }

    //删除一个系统用户
    public function admin_delete()
    {
        $id=Request::instance()->get('id');
        $offest=Db::name('x_system_user')->where('id',$id)->delete();
        if($offest)
        {
            return json(['code'=>1]);
        }
        else{
            return json(['code'=>0]);
        }
    }

    //跳出登录
    public function exit_admin()
    {
        session_unset();
        $this->success('登出成功','/admin/login/index');
    }
}
